package com.pactera.jep.service.sys.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.pactera.jep.core.code.PermitType;
import com.pactera.jep.core.code.Status;
import com.pactera.jep.core.context.Context;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.sys.code.DashLayoutDesignType;
import com.pactera.jep.sys.dash.widget.Widget;
import com.pactera.jep.sys.dash.widget.WidgetFactory;
import com.pactera.jep.sys.model.DashLayout;
import com.pactera.jep.sys.model.DashLayoutConfig;
import com.pactera.jep.sys.model.DashWidget;
import com.pactera.jep.sys.model.Position;
import com.pactera.jep.sys.service.*;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.entity.NameValuePair;
import com.pactera.jep.web.util.NVUtils;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 布局设计
 * @author xupan
 *
 */
@Api("布局设计")
@RestController
@RequestMapping("/dashLayoutDesign")
public class DashLayoutDesignRestController {
	private static Logger	logger	= LoggerFactory.getLogger(DashLayoutDesignRestController.class);

	@Autowired
	private DashWidgetService dashWidgetService;

	@Autowired
	private PositionService positionService;
	
	@Autowired
	private DashWidgetDakService  dashWidgetDakService;
	
	@Autowired
	private DashLayoutConfigService dashLayoutConfigService;
	
	@Autowired
	private DashLayoutMapService dashLayoutMapService;
	
	@Autowired
	private DashLayoutService dashLayoutService;
	
	/**
	 * 进入布局设计页面
	 * @return 
	 */
	@RequestMapping(value = "/design")
	public String list(String title,String layoutCode,Model model) {
		model.addAttribute("title", title);
		model.addAttribute("layoutCode", layoutCode);
		return "sys/dashLayoutDesign";
	}
	
	/**
	 * 布局设计页面，下拉框显示启用的组件
	 * @return
	 */
	@RequestMapping("/getAllSelectWidget")
	@ResponseBody
	public List<NameValuePair> getAllSelectWidget(){
		List<Filter> filters=new ArrayList<Filter>();
		filters.add(new Filter("status",Status.ENABLED.getValue()));
		List<DashWidget> dashWidgets = dashWidgetService.select(filters);
		return NVUtils.convert(dashWidgets, dashWidgets.size(), "code", "name");
	}
	
	/**
	 * 请求经过解析的组件内容,html代码，该方法应该处理组件的数据权限
	 * @param widgetCode
	 * @return
	 */
	@RequestMapping(value="/getWidgetContent/{widgetCode}")
	@ResponseBody
	public Map<String,Object> getWidgetContent(@PathVariable String widgetCode){
		Map<String,Object> result = new HashMap<String,Object>();
		//封装查询的角色编码和角色组编码
		List<String> dakUsingCodes = new ArrayList<String>();
		try 
		{
			//获取当前登录用户
//			UserProfile userProfile = UserProfileUtils.getUserProfile();
			//获取当前用户所属的角色,角色组
			List<Position> positions = positionService.getAllPositionByStaffCode(Context.getIdentityContext().getIdentity().getId());
			for (Position position : positions)
			{
				dakUsingCodes.add(position.getRoleCode());
				dakUsingCodes.add(position.getRoleGroupCode());
			}
			//获取当前用户最大权限
			PermitType permitType = dashWidgetDakService.queryMaxDashWidgetDak(dakUsingCodes, widgetCode);
			Widget widget = WidgetFactory.createWidget(widgetCode, permitType);
			String content = widget.newContent();
			result.put("content", content);
			result.put(WebCode.SUCCESS,true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			result.put(WebCode.SUCCESS,false);
		}
		return result;
	}
	
	/**
	 * 保存布局设计页面的数据
	 * @param dashLayoutConfig
	 * @return
	 */
	@RequestMapping(value="/saveLayoutDesignData")
	@ResponseBody
	public Map<String,Object> saveLayoutDesignData(DashLayoutConfig dashLayoutConfig){
		Map<String,Object> resultMap=new HashMap<String,Object>();
		try {
			String result= dashLayoutConfigService.saveDashLayoutConfig(dashLayoutConfig);
			//如果需要在不关闭窗口的情况下多次更新，则需要向页面回写version，打开下面的代码
			int version = dashLayoutConfigService.getVersion(dashLayoutConfig);
			if(WebCode.OUT_OF_DATE.equals(result)){
				resultMap.put(WebCode.SUCCESS,false);
				resultMap.put(WebCode.MSG,WebCode.FAILURE_OUT_OF_DATE);
			}else{
				resultMap.put(WebCode.SUCCESS,true);
				resultMap.put(WebCode.MSG,WebCode.SUCCESS_SAVE);
				//如果需要在不关闭窗口的情况下多次更新，则需要向页面回写version，打开下面的代码
				resultMap.put("version", version);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			resultMap.put(WebCode.SUCCESS,false);
			resultMap.put(WebCode.MSG,WebCode.FAILURE_SAVE);
		}
		
		return resultMap;
	}
	
	/**
	 * 首页获取布局的配置信息
	 * @return
	 */
	@RequestMapping(value="/getHomePageLayoutConfig")
	@ResponseBody
	public Map<String,Object> getHomePageLayoutConfig(Model model){
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			//获取当前登录用户最优先的布局
//			UserProfile userProfile = UserProfileUtils.getUserProfile();
			DashLayoutConfig layoutConfig = dashLayoutMapService.getLayoutConfig(Context.getIdentityContext().getIdentity().getId());
			if(layoutConfig != null)
			{
				DashLayout dashLayout = dashLayoutService.get(layoutConfig.getLayoutCode());
				if(DashLayoutDesignType.NORMAL.getValue().equals(dashLayout.getDesignType()))
				{
					JSONObject json = null;
					json = JSONObject.parseObject(layoutConfig.getConfig());
					data.put("config", json);
				}
				else
				{
					data.put("config", layoutConfig.getConfig());
				}
				
				data.put("version", layoutConfig.getVersion());
				data.put("designType", dashLayout.getDesignType());
				data.put(WebCode.SUCCESS, true);
			}
			data.put(WebCode.SUCCESS, false);
		} 
		catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			data.put(WebCode.SUCCESS, false);
		}
		return data;
	}
}